<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/21 0021
 * Time: 下午 10:38
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Model\ShopGoodsClassify;
use Illuminate\Http\Request;

class ShopGoodsClassifyController extends BaseController
{
    public function Index()
    {
        return view('Admin/shopClass');
    }

    public function GetAddClassify()
    {
        return view('Admin/ShopGoodsClassify.add');
    }

    public function PostAddClassify(Request $request)
    {
        $data = $request->all();
        switch ($data['class']){
            case 'select': //查询
                $rico = ShopGoodsClassify::where(['pid' => 0 ])->orderBy('displayorder','asc')->get()->toArray();
                foreach ($rico as $k =>$v){
                    $rico[$k]['tow_data']=ShopGoodsClassify::where( ['pid' => $v['id'] ] )->orderBy('displayorder','asc')->get()->toArray();
                }
                if ($rico){
                    return ['code'=>1,'data'=>$rico];
                } else {
                    return ['code'=>0,'data'=>'没有数据'];
                }
                break;
            case 'select_cid': //查询商品一级分类
                $rico = ShopGoodsClassify::where(['pid' => 0 ])->get()->toArray();
                if ($rico){
                    return ['code'=>1,'data'=>$rico];
                } else {
                    return ['code'=>0,'data'=>'没有数据'];
                }
                break;
            case 'two_tid': //查询商品二级分类
                $rico = ShopGoodsClassify::where( ['pid' => $data['cid'] ] )->get()->toArray();
                if ($rico){
                    return ['code'=>1,'data'=>$rico];
                } else {
                    return ['code'=>0,'data'=>'没有数据'];
                }
                break;
            case 'add':    //添加 or 修改
                unset($data['category']['tow_data']);
                if ($data['category']['id']) { //修改
                    $Rows = ShopGoodsClassify::where('id',$data['category']['id'])->update($data['category']);
                } else {
                    $data['category']['pid'] = empty($data['category']['pid'])?0:$data['category']['pid'];
                    $Rows = ShopGoodsClassify::create($data['category']);
                }
                if ($Rows){
                    return ['code'=>1,'data'=>'设置成功'];
                } else {
                    return ['code'=>0,'data'=>'设置失败'];
                }
                break;
            case 'find':   //查询一条数据
                $type = $data['type']; //1 直商城 2 微商城 3 0元商城
                $pid = empty($data['pid'])?0:$data['pid'];
                $table = ShopGoodsClassify::where(['type'=>$type]);
                $table->where(['pid'=>$pid]);
                $rico = $table->get()->toArray();
                if ($rico){
                    return ['code'=>1,'data'=>$rico];
                } else {
                    return ['code'=>0,'data'=>'设置失败'];
                }
                break;
            case 'delete': //删除
                $count = ShopGoodsClassify::where('pid',$data['id'])->count();
                if ($count) {
                    return ['code'=>0,'data'=>'有子分类不能删除'];
                } else {
                    $rico = ShopGoodsClassify::where('id',$data['id'])->delete();
                    if ($rico){
                        return ['code'=>1,'data'=>'删除成功'];
                    } else {
                        return ['code'=>0,'data'=>'删除失败'];
                    }
                }
                break;
        }
    }



    public function getEditClassify($id){
        $Classify = ShopGoodsClassify::where('id',$id)->first();
        return view('ShopGoodsClassify.edit',['classify'=>$Classify]);
    }

    public function PostEditClassify(Request $request)
    {
        $P_id = $request->input('p_id');
        $Name = $request->input('name');
        $Id = $request->input('id');
        $Classify = ShopGoodsClassify::where('id',$Id)->first();
        $Classify->p_id = $P_id;
        $Classify->name = $Name;
        $Rows = $Classify->save();
        if ($Rows){
            return redirect('/admin/classify')->with('edit_classify','商品分类跟新成功');
        }
    }

    public function DelClassify($id)
    {
        $Count = ShopGoodsClassify::where('p_id',$id)->get()->count();
        if ($Count > 0){
            return redirect()->back()->withErrors('当前分类有子类,删除不了');
        }else{
            if (ShopGoodsClassify::where('p_id',$id)->delete()){
                return redirect('/admin/classify')->with('del_classify','商品分类删除成功');
            }
        }

    }



//    public function ()
//    {
//
//    }

}